﻿using Microsoft.Xna.Framework;
using System;

namespace block_explorer
{
    class DriveableEntity : BaseEntity
    {
        protected Vector3 mDriveForce;

        public float DrivePower { get; set; }

        public DriveableEntity()
        {
        }

        public override void Update(GameTime gameTime)
        {
            if (mDriveForce.LengthSquared() > Single.Epsilon)
                ApplyDriveForce(gameTime);

            base.Update(gameTime);
        }

        public override void OnInputCommand(InputCommand command)
        {
            switch (command.Type)
            {
                case InputCommand.Command.MoveForward:
                    mDriveForce.Z -= command.Event == InputCommand.ContinuousEvent.Start ? 1.0f : -1.0f;
                    break;
                case InputCommand.Command.MoveBackward:
                    mDriveForce.Z += command.Event == InputCommand.ContinuousEvent.Start ? 1.0f : -1.0f;
                    break;
                case InputCommand.Command.MoveLeft:
                    mDriveForce.X -= command.Event == InputCommand.ContinuousEvent.Start ? 1.0f : -1.0f;
                    break;
                case InputCommand.Command.MoveRight:
                    mDriveForce.X += command.Event == InputCommand.ContinuousEvent.Start ? 1.0f : -1.0f;
                    break;
                case InputCommand.Command.MoveUp:
                    mDriveForce.Y += command.Event == InputCommand.ContinuousEvent.Start ? 1.0f : -1.0f;
                    break;
                case InputCommand.Command.MoveDown:
                    mDriveForce.Y -= command.Event == InputCommand.ContinuousEvent.Start ? 1.0f : -1.0f;
                    break;
            }
        }

        protected virtual void ApplyDriveForce(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Vector3 transformedForce = Vector3.Transform(mDriveForce, Quaternion.Inverse(Orientation));
            Velocity += transformedForce * DrivePower * dt;
        }
    }
}
